#include <GL/glut.h>
#include "3dApp.h"
#include<iostream>

using namespace std;

/*------------------------------global variables--------------------------------------*/
//transformation constants
#define TRANSFORM_NONE		0	 
#define TRANSFORM_ROTATE	1
#define TRANSFORM_SCALE	   	2 
#define TRANSFORM_TRANSLATE 3

//display mode constants
#define OBJ_WIREFRAME	0
#define OBJ_FLAT		1
#define OBJ_SMOOTH		2 
#define OBJ_POINT		3

//static variables used for menu
static int window;	
static int primitive = 0;

//Static variables used for transformation
static int press_x, press_y; 
static float x_angle = 0.0; 
static float y_angle = 0.0;  
static float scale_size = 1;
static float x_meas=0.0;
static float y_meas=0.0;

static int obj_mode = 0;
static int xtform_mode = 0; 

float aspect_ratio = 1.0;

void myMouse(int button, int state, int x, int y)
{
	if (state == GLUT_DOWN) 
	{
		press_x = x; press_y = y; 
		if (button == GLUT_LEFT_BUTTON)
			xtform_mode = TRANSFORM_ROTATE; 
		else if (button == GLUT_MIDDLE_BUTTON) 
			xtform_mode = TRANSFORM_TRANSLATE; 
		else if (button == GLUT_RIGHT_BUTTON)
			xtform_mode = TRANSFORM_SCALE;
	}
	else if (state == GLUT_UP) 
	{
		xtform_mode = TRANSFORM_NONE; 
	}
}

void myTransform(int x, int y)
{
	if (xtform_mode == TRANSFORM_ROTATE) 
	{
		x_angle += (x - press_x)/5.0; 

		if (x_angle > 180) 
			x_angle -= 360; 
		else if (x_angle <-180) 
			x_angle += 360; 
      
		press_x = x; 
	   
		y_angle += (y - press_y)/5.0; 

		if (y_angle > 180) 
			y_angle -= 360; 
		else if (y_angle <-180) 
			y_angle += 360; 
      
		press_y = y; 
    }
	else if (xtform_mode == TRANSFORM_SCALE)
	{
		float old_size = scale_size;
		
		scale_size *= (1 + (y - press_y)/1000.0); 

		if (scale_size <0) 
			scale_size = old_size; 
		press_y = y; 
      }
	else if (xtform_mode == TRANSFORM_TRANSLATE)
     {
          x_meas -= (x-press_x)/500.0;
          press_x=x;
          y_meas += (y-press_y)/500.0;
          press_y=y;
     }    
	glutPostRedisplay(); 
}

void reshape(int w, int h)
{
	//Set the view port to entire window
	glViewport(0, 0, w, h);
	//Set the correct aspect ratio
	aspect_ratio = float(w)/float(h);
	glutPostRedisplay(); // force the redraw OpenGL (or GLUT)
}

void drawModel(Object &obj){
	int i,j;
	glEnable(GL_LIGHTING);
	glEnable(GL_LIGHT0);
	for(i=0;i<obj.faces.size();i++)
	{
		glBegin(GL_TRIANGLES);
		glColor3f(0,1,0);
			
			//glNormal3f(obj.vertices[obj.faces[i].v1].n[0],obj.vertices[obj.faces[i].v1].n[1],obj.vertices[obj.faces[i].v1].n[2]);
			glVertex3f(obj.vertices[obj.faces[i].v1].v[0],obj.vertices[obj.faces[i].v1].v[1],obj.vertices[obj.faces[i].v1].v[2]);

			//glNormal3f(obj.vertices[obj.faces[i].v2].n[0],obj.vertices[obj.faces[i].v2].n[1],obj.vertices[obj.faces[i].v2].n[2]);
			glVertex3f(obj.vertices[obj.faces[i].v2].v[0],obj.vertices[obj.faces[i].v2].v[1],obj.vertices[obj.faces[i].v2].v[2]);

			//glNormal3f(obj.vertices[obj.faces[i].v3].n[0],obj.vertices[obj.faces[i].v3].n[1],obj.vertices[obj.faces[i].v3].n[2]);
			glVertex3f(obj.vertices[obj.faces[i].v3].v[0],obj.vertices[obj.faces[i].v3].v[1],obj.vertices[obj.faces[i].v3].v[2]);
	}
		glEnd();
}

void drawNewModel(Object &obj){
	int i,j;
	glDisable(GL_LIGHTING);
	glDisable(GL_LIGHT0);
	for(i=0;i<obj.faces.size();i++)
	{
		glBegin(GL_LINES);
			
			//glNormal3f(obj.newVertices[obj.faces[i].v1].n[0],obj.newVertices[obj.faces[i].v1].n[1],obj.newVertices[obj.faces[i].v1].n[2]);
			glVertex3f(obj.newVertices[obj.faces[i].v1].v[0],obj.newVertices[obj.faces[i].v1].v[1],obj.newVertices[obj.faces[i].v1].v[2]);
			glVertex3f(obj.newVertices[obj.faces[i].v2].v[0],obj.newVertices[obj.faces[i].v2].v[1],obj.newVertices[obj.faces[i].v2].v[2]);

			//glNormal3f(obj.newVertices[obj.faces[i].v2].n[0],obj.newVertices[obj.faces[i].v2].n[1],obj.newVertices[obj.faces[i].v2].n[2]);
			glVertex3f(obj.newVertices[obj.faces[i].v2].v[0],obj.newVertices[obj.faces[i].v2].v[1],obj.newVertices[obj.faces[i].v2].v[2]);
			glVertex3f(obj.newVertices[obj.faces[i].v3].v[0],obj.newVertices[obj.faces[i].v3].v[1],obj.newVertices[obj.faces[i].v3].v[2]);

			//glNormal3f(obj.newVertices[obj.faces[i].v3].n[0],obj.newVertices[obj.faces[i].v3].n[1],obj.newVertices[obj.faces[i].v3].n[2]);
			glVertex3f(obj.newVertices[obj.faces[i].v3].v[0],obj.newVertices[obj.faces[i].v3].v[1],obj.newVertices[obj.faces[i].v3].v[2]);
			glVertex3f(obj.newVertices[obj.faces[i].v1].v[0],obj.newVertices[obj.faces[i].v1].v[1],obj.newVertices[obj.faces[i].v1].v[2]);
	}
		glEnd();
}

